/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Constraint/IRBE3Repository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"

SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace RBE3
{
    struct IDENTITY
    {
        SG::DataStructure::Common::I64  EID;
        SG::DataStructure::Common::I64  REFG;
        SG::DataStructure::Common::I64  REFC;
        SG::DataStructure::Common::I64  WTCG_POS;
        SG::DataStructure::Common::I64  WTCG_LEN;
        SG::DataStructure::Common::I64  GM_POS;
        SG::DataStructure::Common::I64  GM_LEN;
        SG::DataStructure::Common::Real ALPHA;
        SG::DataStructure::Common::Real TREF;
        SG::DataStructure::Common::I64  DOMAIN_ID;
    };

    struct G
    {
        SG::DataStructure::Common::I64 ID;

        // 默认构造函数
        G () : ID (0)
        {}

        G (SG::DataStructure::Common::I64 _id) : ID (_id)
        {}
    };

    struct WTCG
    {
        SG::DataStructure::Common::Real WT1;
        SG::DataStructure::Common::I64  C;
        SG::DataStructure::Common::I64  G_POS;
        SG::DataStructure::Common::I64  G_LEN;

        // 默认构造函数
        WTCG () : WT1 (0.0), C (0), G_POS (0), G_LEN (0)
        {}

        // 带参数的构造函数
        WTCG (SG::DataStructure::Common::Real WT1,
              SG::DataStructure::Common::I64  C,
              SG::DataStructure::Common::I64  G_POS,
              SG::DataStructure::Common::I64  G_LEN)
            : WT1 (WT1), C (C), G_POS (G_POS), G_LEN (G_LEN)
        {}
    };
};  // namespace RBE3
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::RBE3Data::GM> ()
{
    HighFive::CompoundType compoundType = {
        { "GM", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "CM", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::RBE3::G> ()
{
    HighFive::CompoundType compoundType = { { "ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::RBE3::IDENTITY> ()
{
    HighFive::CompoundType compoundType = {
        { "EID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "REFG", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "REFC", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "WTCG_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "WTCG_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "GM_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "GM_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "ALPHA", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "TREF", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::RBE3::WTCG> ()
{
    HighFive::CompoundType compoundType = {
        { "WT1", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "C", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "G_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "G_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN

class RBE3Repository : public IRBE3Repository, public MainlyRepositoryBase<SG::DataStructure::FEM::RBE3Data>
{
  public:
    explicit RBE3Repository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDatasetWTCG,
        SubDatasetGM,
        G
    };

    /**
     * @brief   : 初始化表数据
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    std::vector<SG::DataStructure::FEM::RBE3Data> GetAllData ();

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<SG::DataStructure::FEM::RBE3Data>&          own,
                       std::vector<SG::DBManager::HDF5::RBE3::IDENTITY>& _OUT  IDENTITYData,
                       std::vector<SG::DBManager::HDF5::RBE3::WTCG>& _OUT      wtcgData,
                       std::vector<SG::DataStructure::FEM::RBE3Data::GM>& _OUT gmData,
                       std::vector<SG::DBManager::HDF5::RBE3::G>& _OUT         gData,
                       bool                                                    isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     */
    void NastranToOwn (std::vector<SG::DBManager::HDF5::RBE3::IDENTITY>&   IDENTITYData,
                       std::vector<SG::DBManager::HDF5::RBE3::WTCG>&       wtcgData,
                       std::vector<SG::DataStructure::FEM::RBE3Data::GM>&  gmData,
                       std::vector<SG::DBManager::HDF5::RBE3::G>&          gData,
                       std::vector<SG::DataStructure::FEM::RBE3Data>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
